home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / plugins / HTMLWindow.jar / horst / HTMLWindow.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-03-18  |  4.1 KB  |  146 lines

  1. package horst;
  2.  
  3. import java.awt.AWTEvent;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Dimension;
  7. import java.awt.Point;
  8. import java.awt.Rectangle;
  9. import javax.swing.JComponent;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.border.EmptyBorder;
  12.  
  13. public class HTMLWindow extends JScrollPane implements HTMLPaneStatusListener {
  14.    protected HTMLPane m_renderer;
  15.    protected String m_findText;
  16.    protected int m_lastFindPos;
  17.  
  18.    public HTMLWindow() {
  19.       this.m_findText = "";
  20.       this.m_renderer = this.createHTMLPane();
  21.       ((JScrollPane)this).setViewportView(this.m_renderer);
  22.       ((JComponent)this).setBackground(Color.white);
  23.       ((JScrollPane)this).getViewport().setBackingStoreEnabled(true);
  24.    }
  25.  
  26.    HTMLWindow(boolean bBorder) {
  27.       this();
  28.       if (!bBorder) {
  29.          ((JComponent)this).setBorder(new EmptyBorder(0, 0, 0, 0));
  30.       }
  31.  
  32.    }
  33.  
  34.    protected HTMLPane createHTMLPane() {
  35.       return new HTMLPane();
  36.    }
  37.  
  38.    public int find(String text) {
  39.       int pos = this.m_renderer.findText(text, -1);
  40.       if (pos > -1) {
  41.          this.m_lastFindPos = pos;
  42.          this.m_findText = text;
  43.          this.highlightText(pos);
  44.       }
  45.  
  46.       return pos;
  47.    }
  48.  
  49.    public int findNext() {
  50.       int pos = this.m_renderer.findText(this.m_findText, this.m_lastFindPos);
  51.       if (pos > this.m_lastFindPos) {
  52.          this.m_lastFindPos = pos;
  53.          this.highlightText(pos);
  54.       }
  55.  
  56.       return pos;
  57.    }
  58.  
  59.    public HTMLPane getHTMLPane() {
  60.       return this.m_renderer;
  61.    }
  62.  
  63.    protected void highlightText(int pos) {
  64.       View v = this.m_renderer.m_rootView.modelToView(pos);
  65.       if (v != null) {
  66.          this.m_renderer.m_document.m_selectedP0 = pos;
  67.          this.m_renderer.m_document.m_selectedP1 = pos + this.m_findText.length() - 1;
  68.          Rectangle vb = v.getBounds();
  69.          Rectangle vr = ((JScrollPane)this).getViewport().getViewRect();
  70.          if (vr.x <= vb.x && vr.y <= vb.y && vr.x + vr.width >= vb.x + vb.width && vr.y + vr.height >= vb.y + vb.height) {
  71.             ((Component)this).repaint();
  72.          } else {
  73.             this.scrollToPosition(pos);
  74.          }
  75.       }
  76.  
  77.    }
  78.  
  79.    public void scrollToPosition(int pos) {
  80.       View v = this.m_renderer.m_rootView.modelToView(pos);
  81.       if (v != null) {
  82.          Rectangle r = v.getBounds();
  83.          if (r != null) {
  84.             Rectangle docRect = this.m_renderer.m_rootView.getBounds();
  85.             Dimension portDim = ((JScrollPane)this).getViewport().getExtentSize();
  86.             int yOffset = 20;
  87.             int xOffset = 20;
  88.             int portY = Math.max(0, r.y - yOffset);
  89.             int portX = Math.max(0, r.x - xOffset);
  90.             if (portX + portDim.width > docRect.width) {
  91.                int diff = portX + portDim.width - docRect.width;
  92.                portX -= diff;
  93.                if (portX < 0) {
  94.                   portX = 0;
  95.                }
  96.             }
  97.  
  98.             if (portY + portDim.height > docRect.height) {
  99.                int diff = portY + portDim.height - docRect.height;
  100.                portY -= diff;
  101.                if (portY < 0) {
  102.                   portY = 0;
  103.                }
  104.             }
  105.  
  106.             ((JScrollPane)this).getViewport().setViewPosition(new Point(portX, portY));
  107.          }
  108.       }
  109.  
  110.    }
  111.  
  112.    public void scrollToVisible(int pos) {
  113.       View v = this.m_renderer.m_rootView.modelToView(pos);
  114.       if (v != null) {
  115.          Rectangle vb = v.getBounds();
  116.          Rectangle vr = ((JScrollPane)this).getViewport().getViewRect();
  117.          if (vr.x <= vb.x && vr.y <= vb.y && vr.x + vr.width >= vb.x + vb.width && vr.y + vr.height >= vb.y + vb.height) {
  118.             ((Component)this).repaint();
  119.          } else {
  120.             this.scrollToPosition(pos);
  121.          }
  122.       }
  123.  
  124.    }
  125.  
  126.    public void scrollToYPosition(int y) {
  127.       Point pt = ((JScrollPane)this).getViewport().getViewPosition();
  128.       ((JScrollPane)this).getViewport().setViewPosition(new Point(pt.x, y));
  129.       ((JScrollPane)this).getViewport().repaint();
  130.    }
  131.  
  132.    public void setHTMLPane(HTMLPane pane) {
  133.       this.m_renderer = pane;
  134.       ((JScrollPane)this).setViewportView(this.m_renderer);
  135.    }
  136.  
  137.    public boolean statusChanged(HTMLPaneStatusEvent evt) {
  138.       switch (((AWTEvent)evt).getID()) {
  139.          case 5:
  140.             this.m_lastFindPos = 0;
  141.          default:
  142.             return true;
  143.       }
  144.    }
  145. }
  146.